package edu.scau.web.pos;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.core.constants.AjaxResult;
import edu.scau.core.constants.HttpStatus;
import edu.scau.pos.domain.SaleLineItem;
import edu.scau.pos.service.SaleLineItemService;
import edu.scau.pos.vo.SaleItemVo;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Controller
 */
@RestController
@Hidden
@RequestMapping("/salelineitem")
public class SalelineitemController {

    @Autowired
    private SaleLineItemService saleLineItemService;

    @Operation(
        summary = "查找所有",
        description = "查找所有未删除的信息",
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "400", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SaleItemVo.class)))
        }
    )
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        List<SaleItemVo> salelineitemList = saleLineItemService.listAll();
        return AjaxResult.success(salelineitemList);
    }


    @Operation(
        summary = "listByOrderId",
        description = "根据ID查找信息",
        parameters = {
            @Parameter(name = "salelineitemId", description = "ID", required = true, schema = @Schema(type = "Long"))
        },
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SaleLineItem.class))),
            @ApiResponse(responseCode = "204", description = "不存在", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @GetMapping("/findSalelineitemById")
    public AjaxResult listByOrderId(@RequestParam("orderId") String orderId) {
        List<SaleItemVo> salelineitemList = saleLineItemService.listByOrderId(orderId);
        return AjaxResult.success(salelineitemList);
    }

    @Operation(
        summary = "更新",
        description = "根据ID更新信息",
        requestBody = @RequestBody(
            description = "更新条件,id是必须的",
            required = true,
            content = @Content(
                mediaType = "application/json",
                schema = @Schema(implementation = SaleItemVo.class)
            )
        ),
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "更新失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @PutMapping("/update")
    public AjaxResult updateSalelineitem(@org.springframework.web.bind.annotation.RequestBody SaleLineItem saleLineItem) {
        int res = saleLineItemService.updateSalelineitem(saleLineItem);
        return res > 0 ? AjaxResult.success(HttpStatus.SUCCESS) : AjaxResult.error("更新失败");
    }

    @Operation(
        summary = "插入",
        description = "插入信息",
        requestBody = @RequestBody(
            description = "插入条件",
            required = true,
            content = @Content(
                mediaType = "application/json",
                schema = @Schema(implementation = SaleItemVo.class)
            )
        ),
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "插入失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @PutMapping("/insert")
    public AjaxResult insertSalelineitem(@org.springframework.web.bind.annotation.RequestBody SaleLineItem saleLineItem) {
        int res = saleLineItemService.insertSalelineitem(saleLineItem);
        return res > 0 ? AjaxResult.success(200) : AjaxResult.error("插入失败！");
    }

    @Operation(
        summary = "根据ID删除",
        description = "根据ID删除信息",
        parameters = {
            @Parameter(name = "salelineitemId", description = "ID", required = true, schema = @Schema(type = "Long"))
        },
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "删除失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @DeleteMapping("/deleteSalelineitemByOrderId")
    public AjaxResult deleteSalelineitemByOrderId(@RequestParam("orderId") String orderId) {
        int res =saleLineItemService.deleteSalelineitemByOrderId(orderId);
        return res > 0 ? AjaxResult.success("删除成功", 200) : AjaxResult.error("删除失败");
    }

    @PostMapping("/insertBatch")
    public AjaxResult insertBatch(@org.springframework.web.bind.annotation.RequestBody List<SaleLineItem> saleItemVos){
        int res=saleLineItemService.insertBatch(saleItemVos);
        return AjaxResult.success();
    }
}
